﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.InfoManage
{
    public partial class WuLiaoBianJi : BaseForm
    {
        private DataGridViewRow m_SelectRow = null;
        private bool m_IsRuKu = false;

        public WuLiaoBianJi(bool IsRuKu)
        {
            InitializeComponent();
            m_IsRuKu = IsRuKu;
        }

        public WuLiaoBianJi(DataGridViewRow SelectRow,bool IsRuKu)
        {
            InitializeComponent();
            m_SelectRow = SelectRow;
            m_IsRuKu = IsRuKu;
        }

        private void WuLiaoBianJi_Load(object sender, EventArgs e)
        {
            if (m_IsRuKu)
            {
                cmb_wuliaoleixing.SelectedIndex = 0;
            }
            else
            {
                cmb_wuliaoleixing.SelectedIndex = 1;
            }

            if (m_SelectRow != null)
            {
                txt_wuliaomingcheng.Text = string.Format("{0}", m_SelectRow.Cells["col_wuliaomingcheng"].Value);
                cmb_wuliaoleixing.SelectedItem = string.Format("{0}", m_SelectRow.Cells["col_wuliaoleixing"].Value);
                chk_wuliaozhuangtai.Checked = m_SelectRow.Cells["col_wuliaozhuangtai"].Value.Equals("启用") ? true : false;
                chk_shezhifahuozhongliang.Checked = m_SelectRow.Cells["col_shezhifahuozhongliang"].Value.Equals("启用") ? true : false;
                chk_shezhiquxiang.Checked = m_SelectRow.Cells["col_shezhiquxiang"].Value.Equals("启用") ? true : false;
            }
        }


        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_wuliaomingcheng.Text))
            {
                KeaoUtils.ShowInfoMessage("物料名称不能为空，请重试。");
                return;
            }


            if (m_SelectRow == null)
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("wuliaomingcheng", DataValueCommonType.字符串, txt_wuliaomingcheng.Text, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, cmb_wuliaoleixing.Text, "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_wuliao", createCondition);
                if (Dt == null)
                {
                    return;
                }
                if (Dt.Rows.Count > 0)
                {
                    KeaoUtils.ShowInfoMessage("该物料名称已经存在，不能重复保存。");
                    return;
                }

                TableRow newRow = new TableRow("tbl_wuliao", KeaoUtils.GetGUID(), DBCtl.插入);
                newRow.DataCols.Add(new TableCol() { ColName="wuliaomingcheng", ValueMember=txt_wuliaomingcheng.Text, ValueType= DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "wuliaoleixing", ValueMember = cmb_wuliaoleixing.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "wuliaozhuangtai", ValueMember = chk_wuliaozhuangtai.Checked?"启用":"禁用", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "shezhiquxiang", ValueMember = chk_shezhiquxiang.Checked?"启用":"禁用", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "shezhifahuozhongliang", ValueMember = chk_shezhifahuozhongliang.Checked?"启用":"禁用", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                {
                    DialogResult = DialogResult.OK;
                }
            }
            else
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("wuliaomingcheng", DataValueCommonType.字符串, txt_wuliaomingcheng.Text, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, cmb_wuliaoleixing.Text, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", m_SelectRow.Cells["col_id"].Value), "", SearchConditionType.不等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_wuliao", createCondition);
                if (Dt == null)
                {
                    return;
                }
                if (Dt.Rows.Count > 0)
                {
                    KeaoUtils.ShowInfoMessage("该物料名称已经存在，不能重复保存。");
                    return;
                }

                TableRow editRow = new TableRow("tbl_wuliao", string.Format("{0}",m_SelectRow.Cells["col_id"].Value), DBCtl.修改);
                editRow.DataCols.Add(new TableCol() { ColName = "wuliaomingcheng", ValueMember = txt_wuliaomingcheng.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "wuliaoleixing", ValueMember = cmb_wuliaoleixing.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "wuliaozhuangtai", ValueMember = chk_wuliaozhuangtai.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "shezhiquxiang", ValueMember = chk_shezhiquxiang.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "shezhifahuozhongliang", ValueMember = chk_shezhifahuozhongliang.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                {
                    DialogResult = DialogResult.OK;
                }
            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

    }
}
